<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>手写promise</title>
</head>
<body>
    <ol>
        <li>实现一个promise，在 setTimeout 中去 resolve</li>
    </ol>
    <script>
        class MyPromise{
            constructor(fn) {
                this.resolveCallbacks = [];

                const resolve = val => {
                    this.resolveCallbacks.map(fn => fn(val))
                }

                const reject = () => {

                }

                fn(resolve,reject);
            }

            then(onFulfilled){
                this.resolveCallbacks.push(onFulfilled);
            }
        }

        new MyPromise((resolve,reject) => {
            setTimeout(() => {
                resolve('step1');
            },1000)
        }).then(res => {
            console.log(res);
        });
    </script>
</body>
</html>